package com.qyhl.webtv.basiclib.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * Created by helin on 2017/1/10.
 */
public final class DateUtils {

    /**
     * 日期类型 *
     */
    public static final String yyyyMMDD = "yyyy-MM-dd";
    public static final String date_ymd = "yyyy年MM月dd日";
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String MMddHHmm = "MM-dd HH:mm";
    public static final String HHmmss = "HH:mm:ss";
    public static final String mmss = "mm:ss";
    public static final String LOCALE_DATE_FORMAT = "yyyy年M月d日 HH:mm:ss";
    public static final String DB_DATA_FORMAT = "yyyy-MM-DD HH:mm:ss";
    public static final String NEWS_ITEM_DATE_FORMAT = "hh:mm M月d日 yyyy";
    public static final String HHmm = "HH:mm";
    public static final String yyyMMddHHmm = "yyyy年M月d日 HH时mm分";

    private static final long ONE_MINUTE = 60000L;

    private static final long ONE_HOUR = 3600000L;

    private static final long ONE_DAY = 86400000L;

    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";

    private static final String ONE_MINUTE_AGO = "分钟前";

    private static final String ONE_HOUR_AGO = "小时前";

    private static final String ONE_DAY_AGO = "天前";

    private static final String ONE_MONTH_AGO = "月前";

    private static final String ONE_YEAR_AGO = "年前";

    @SuppressLint("SimpleDateFormat")
    public static String brokeFormat(Date date) {
        long delta = new Date().getTime() - date.getTime();
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天";
        }
        if (delta < 30L * ONE_DAY) {
            long days = toDays(delta);
            return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
        }


        SimpleDateFormat format;
        if (delta < 12L * 4L * ONE_WEEK) {
            format = new SimpleDateFormat("MM-dd HH:mm");
        } else {
            format = new SimpleDateFormat("yyyy-MM-dd");

        }
        return format.format(date);
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {

        return toSeconds(date) / 60L;

    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

    @SuppressLint("SimpleDateFormat")
    public static String brokeTranslate(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = format.parse(time);
            return brokeFormat(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }


    @SuppressLint("SimpleDateFormat")
    public static String getYyyyMMDD() {
        try {
            SimpleDateFormat df = new SimpleDateFormat(yyyyMMDD);
            long curTime = System.currentTimeMillis();
            return df.format(curTime);
        } catch (Exception e) {
            return "";
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static String getHHmm() {
        try {
            SimpleDateFormat df = new SimpleDateFormat(HHmm);
            long curTime = System.currentTimeMillis();
            return df.format(curTime);
        } catch (Exception e) {
            return "";
        }
    }


    /**
     * 判断当前时间是否在指定时间前
     *
     * @param time 指定时间
     * @return 结果
     */
    public static boolean isBeforeTime(String time) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long indexTime = simpleDateFormat.parse(time).getTime();
            return System.currentTimeMillis() < indexTime;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }


    public static String translateToHHmmss(String date) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(HHmmss);
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            if (StringUtils.isEmpty(date)) {
                return "";
            } else {
                return date;
            }
        }
    }

    public static String translateToYMd(String date) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(yyyyMMDD);
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            if (StringUtils.isEmpty(date)) {
                return "";
            } else {
                return date;
            }
        }
    }


    public static String translateToShopping(String date) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(yyyMMddHHmm);
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            if (StringUtils.isEmpty(date)) {
                return "";
            } else {
                return date;
            }
        }
    }

    public static String getMessageDateFormat(String date) {
        try {
            long tDate = new SimpleDateFormat(yyyyMMddHHmmss).parse(date).getTime();
            return new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(tDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }


    @SuppressLint("SimpleDateFormat")
    public static String practiceRecFormat(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat mFormat = new SimpleDateFormat("MM/dd HH:mm");
        if (StringUtils.isNotEmpty(date)) {
            try {
                long tDate = simpleDateFormat.parse(date).getTime();
                return mFormat.format(tDate);
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static String practiceTrendFormat(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat mFormat = new SimpleDateFormat("MM-dd HH:mm");
        if (StringUtils.isNotEmpty(date)) {
            try {
                long tDate = simpleDateFormat.parse(date).getTime();
                return mFormat.format(tDate);
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    public static String PracticeServiceTimeFormat(long second) {
        double halfhour = Math.round((double) second / 1800);
        if (halfhour % 2 == 0) {
            return (int) halfhour / 2 + "";
        } else {
            return halfhour / 2 + "";
        }
    }

    public static String dateToString(long time, String pattern) {
        try {
            return new SimpleDateFormat(pattern).format(new Date(time));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "未知";
    }


    public static String dateToString(Date date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date stringToDate(String dateStr, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将Date类型转换为日期字符串
     *
     * @param date Date对象
     * @param type 需要的日期格式
     * @return 按照需求格式的日期字符串
     */
    public static String formatDate(Date date, String type) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(type);
            return df.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将日期字符串转换为Date类型
     *
     * @param dateStr 日期字符串
     * @param type    日期字符串格式
     * @return Date对象
     */
    public static Date parseDate(String dateStr, String type) {
        SimpleDateFormat df = new SimpleDateFormat(type);
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }

    /**
     * 获取日期
     *
     * @return
     */
    public static String getDate() {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");
            long curTime = System.currentTimeMillis();
            return df.format(curTime);
        } catch (Exception e) {
            return "";
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static String getVoteTime(String date) {
        try {
            long tDate = new SimpleDateFormat("MM.dd HH:ss").parse(date).getTime();
            return new SimpleDateFormat("MM/dd").format(tDate);
        } catch (Exception e) {
            e.printStackTrace();
            return date;
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static String getCurrentTime() {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long curTime = System.currentTimeMillis();
            return df.format(curTime);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 得到年
     *
     * @param date Date对象
     * @return 年
     */
    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    /**
     * 得到月
     *
     * @param date Date对象
     * @return 月
     */
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;

    }

    /**
     * 得到日
     *
     * @param date Date对象
     * @return 日
     */
    public static int getDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }


    public static String translateDateForNotice(String Noticedate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyyMMddHHmmss);
        try {
            Date noticeTime = simpleDateFormat.parse(Noticedate);
            long time = noticeTime.getTime();
            long oneDay = 24 * 60 * 60 * 1000;
            Calendar current = Calendar.getInstance();
            Calendar today = Calendar.getInstance();    //今天

            today.set(Calendar.YEAR, current.get(Calendar.YEAR));
            today.set(Calendar.MONTH, current.get(Calendar.MONTH));
            today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
            //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
            today.set(Calendar.HOUR_OF_DAY, 0);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.SECOND, 0);

            long todayStartTime = today.getTimeInMillis();

            if (time >= todayStartTime && time < todayStartTime + oneDay) { // today
                return "今天";
            } else if (time >= todayStartTime - oneDay && time < todayStartTime) { // yesterday
                return "昨天";
            } else if (time >= todayStartTime - oneDay * 2 && time < todayStartTime - oneDay) { // the day before yesterday
                return "前天";
            } else if (time > todayStartTime + oneDay) { // future
                return "将来某一天";
            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
                Date date = new Date(time);
                return dateFormat.format(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 转换日期 将日期转为今天, 昨天, 前天, XXXX-XX-XX, ...
     *
     * @param time 时间
     * @return 当前日期转换为更容易理解的方式
     */
    public static String translateDate(Long time) {
        long oneDay = 24 * 60 * 60 * 1000;
        Calendar current = Calendar.getInstance();
        Calendar today = Calendar.getInstance();    //今天

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        long todayStartTime = today.getTimeInMillis();

        if (time >= todayStartTime && time < todayStartTime + oneDay) { // today
            return "今天";
        } else if (time >= todayStartTime - oneDay && time < todayStartTime) { // yesterday
            return "昨天";
        } else if (time >= todayStartTime - oneDay * 2 && time < todayStartTime - oneDay) { // the day before yesterday
            return "前天";
        } else if (time > todayStartTime + oneDay) { // future
            return "将来某一天";
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(time);
            return dateFormat.format(date);
        }
    }


    /**
     * 转换日期 转换为更为人性化的时间
     *
     * @param time 时间
     * @return
     */
    public static String translateDate(long time, long curTime) {
        long oneDay = 24 * 60 * 60;
        Calendar today = Calendar.getInstance();    //今天
        today.setTimeInMillis(curTime * 1000);
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        long todayStartTime = today.getTimeInMillis() / 1000;
        if (time >= todayStartTime) {
            long d = curTime - time;
            if (d <= 60) {
                return "1分钟前";
            } else if (d <= 60 * 60) {
                long m = d / 60;
                if (m <= 0) {
                    m = 1;
                }
                return m + "分钟前";
            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("今天 HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!TextUtils.isEmpty(dateStr) && dateStr.contains(" 0")) {
                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            }
        } else {
            if (time < todayStartTime && time > todayStartTime - oneDay) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("昨天 HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!TextUtils.isEmpty(dateStr) && dateStr.contains(" 0")) {

                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            } else if (time < todayStartTime - oneDay && time > todayStartTime - 2 * oneDay) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("前天 HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!TextUtils.isEmpty(dateStr) && dateStr.contains(" 0")) {
                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            } else {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date date = new Date(time * 1000);
                String dateStr = dateFormat.format(date);
                if (!TextUtils.isEmpty(dateStr) && dateStr.contains(" 0")) {
                    dateStr = dateStr.replace(" 0", " ");
                }
                return dateStr;
            }
        }
    }

    /**
     * 新闻列表时间格式
     *
     * @param date
     * @return
     */
    public static String mTranslateDate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
        SimpleDateFormat mouthFormat = new SimpleDateFormat("MM-dd");
        SimpleDateFormat beyondyear = new SimpleDateFormat("yyyy-MM-dd");

        long curTime = System.currentTimeMillis();
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            if (tDate > curTime) {
                return "刚刚";
            } else if (yearFormat.format(tDate).equals(yearFormat.format(curTime))) {
                long dtime = (curTime - tDate) / 1000;
                if (dtime <= 60L) {
                    if (dtime == 0) {
                        return "刚刚";
                    } else {
                        return dtime + "秒前";
                    }
                } else if (dtime <= 3600L) {
                    if (isSameDay(tDate)) {
                        return dtime / 60 + "分钟前";
                    } else {
                        return mouthFormat.format(tDate);
                    }
                } else if (dtime <= 86400L) {
                    if (isSameDay(tDate)) {
                        return dtime / 3600 + "小时前";
                    } else {
                        return mouthFormat.format(tDate);
                    }
                } else {
                    return mouthFormat.format(tDate);
                }
            } else {
                return beyondyear.format(tDate);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return date;
        }
    }

    /**
     * 详情时间转换
     *
     * @param date
     * @return
     */
    public static String detailDateTranslate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
        if (StringUtils.isEmpty(date)) {
            return "";
        }
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    public static String msgDateTranslate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日HH时mm分");

        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    public static String actDate(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat mFormat = new SimpleDateFormat("yyyyMMdd");

        if (StringUtils.isNotEmpty(date)) {
            try {
                long tDate = simpleDateFormat.parse(date).getTime();
                return mFormat.format(tDate);
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * 判断是否是同一天
     *
     * @param time
     * @return
     */
    public static boolean isSameDay(long time) {
        SimpleDateFormat sameday = new SimpleDateFormat("yyyy-MM-dd");
        long curTime = System.currentTimeMillis();
        if (sameday.format(time).equals(sameday.format(curTime))) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isSameDay(long time, long curTime) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sameday = new SimpleDateFormat("yyyy-MM-dd");
        return sameday.format(time).equals(sameday.format(curTime));
    }

    /**
     * 出错的话，返回true
     *
     * @param current 当前需要判断的时间
     * @param before  作为判断的标准
     * @return
     */
    public static boolean isSameDay(String current, String before) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sameday = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long cDate = simpleDateFormat.parse(current).getTime();
            long bDate = simpleDateFormat.parse(before).getTime();
            if (sameday.format(cDate).equalsIgnoreCase(sameday.format(bDate))) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return true;
        }

    }


    public static String originFormat(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat mFormat = new SimpleDateFormat("MM-dd HH:mm");
        if (StringUtils.isNotEmpty(date)) {
            try {
                long tDate = simpleDateFormat.parse(date).getTime();
                return mFormat.format(tDate);
            } catch (ParseException e) {
                e.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static String getCurTime() {
        long time = System.currentTimeMillis();
        String str = String.valueOf(time);
        return str;
    }

    /**
     * 图文直播时间
     *
     * @param date
     * @return
     */
    public static String teleTextFormat(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat yearAndMouthFormat = new SimpleDateFormat("yyyy-MM-dd");
        long curTime = System.currentTimeMillis();
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            if (tDate > curTime) {
                return "未知";
            } else {
                return yearAndMouthFormat.format(tDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "未知";
        }
    }

    //将时间转化成时分的格式
    public static String teleTextDateFormat(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        long curTime = System.currentTimeMillis();
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            if (tDate > curTime) {
                return "未知";
            } else {
                return dateFormat.format(tDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "未知";
        }
    }


    //将分秒格式的转化为时间戳
    public static long timeTranslate(String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
        try {
            return dateFormat.parse(time).getTime();
        } catch (Exception e) {
            return 0;
        }
    }


    /**
     * 判断当前是否是白天，自定义时间段
     *
     * @param from 起始时间 不包括
     * @param to   结束时间爱你  包括
     * @return
     */
    public static boolean isDay(int from, int to) {
        Date date = new Date();
        if (date.getHours() > from && date.getHours() <= to) {
            return true;
        } else {
            return false;
        }
    }

    public static String voiceDuration(long time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(mmss);
        return dateFormat.format(time);
    }

    /**
     * 判断一个时间是否是今天，明天
     *
     * @param date
     * @return
     */
    public static String foreShowTime(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat todayFormat = new SimpleDateFormat("今天 HH:mm 开始");
        SimpleDateFormat tomorrowFormat = new SimpleDateFormat("明天 HH:mm 开始");
        SimpleDateFormat format = new SimpleDateFormat("MM/dd HH:mm 开始");
        SimpleDateFormat yaerFormat = new SimpleDateFormat("yyyy");
        SimpleDateFormat mouthFormat = new SimpleDateFormat("MM");
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd");

        try {
            //所需转换时间
            long tDate = simpleDateFormat.parse(date).getTime();
            //当前时间
            long curTime = System.currentTimeMillis();
            //判断是否是同一年
            if (Integer.parseInt(yaerFormat.format(tDate)) - Integer.parseInt(yaerFormat.format(curTime)) == 0) {
                //判断是否是同一个月
                if (Integer.parseInt(mouthFormat.format(tDate)) - Integer.parseInt(mouthFormat.format(curTime)) == 0) {
                    //判断是否是同一天
                    if (Integer.parseInt(dateFormat.format(tDate)) - Integer.parseInt(dateFormat.format(curTime)) == 0) {
                        if (tDate <= curTime) {
                            return date;
                        } else {
                            return todayFormat.format(tDate);
                        }
                    } else if (Integer.parseInt(dateFormat.format(tDate)) - Integer.parseInt(dateFormat.format(curTime)) < 0) {
                        return date;
                    } else if (Integer.parseInt(dateFormat.format(tDate)) - Integer.parseInt(dateFormat.format(curTime)) == 1) {
                        return tomorrowFormat.format(tDate);
                    } else {
                        return format.format(tDate);
                    }
                } else {
                    return format.format(tDate);
                }
            } else {
                return format.format(tDate);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return date;
        }
    }

    /**
     * 圈子个人主页的时间格式
     *
     * @param date
     * @return
     */
    public static String circleUcenterTime(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat yaerFormat = new SimpleDateFormat("yyyy");
        SimpleDateFormat mouthFormat = new SimpleDateFormat("MM");
        SimpleDateFormat mouthFormat2 = new SimpleDateFormat("M月");
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd");

        //所需转换时间
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            //当前时间
            long curTime = System.currentTimeMillis();
            //判断传入年份在当前年份内(不属于未来时间)
            if (Integer.parseInt(yaerFormat.format(curTime)) - Integer.parseInt(yaerFormat.format(tDate)) > 0) {
                return mouthFormat2.format(tDate) + "|" + dateFormat.format(tDate);
            } else if (Integer.parseInt(yaerFormat.format(curTime)) - Integer.parseInt(yaerFormat.format(tDate)) == 0) {
                //判断月份不属于未来时间
                if (Integer.parseInt(mouthFormat.format(curTime)) - Integer.parseInt(mouthFormat.format(tDate)) > 0) {
                    return mouthFormat2.format(tDate) + "|" + dateFormat.format(tDate);
                } else if (Integer.parseInt(mouthFormat.format(curTime)) - Integer.parseInt(mouthFormat.format(tDate)) == 0) {
                    //判断天数不属于未来时间
                    if (Integer.parseInt(dateFormat.format(curTime)) - Integer.parseInt(dateFormat.format(tDate)) > 0) {
                        if (Integer.parseInt(dateFormat.format(curTime)) - Integer.parseInt(dateFormat.format(tDate)) == 1) {
                            return "昨天";
                        } else {
                            return mouthFormat2.format(tDate) + "|" + dateFormat.format(tDate);
                        }
                    } else if (Integer.parseInt(dateFormat.format(curTime)) - Integer.parseInt(dateFormat.format(tDate)) == 0) {
                        return "今天";
                    } else {
                        return "未知";
                    }
                } else {
                    return "未知";
                }
            } else {
                return "未知";
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "未知";
        }
    }


    /**
     * 明文办报名时间格式
     */
    public static String practiceSignFormat(String date) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    /**
     * 明文办服务时间格式
     */
    public static String practiceServiceFormat(String date) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            if (StringUtils.isEmpty(date)) {
                return "";
            } else {
                return date;
            }
        }
    }

    /**
     * 明文办商品时间格式
     */
    @SuppressLint("SimpleDateFormat")
    public static String practiceShoppingFormat(String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd号 HH:mm:ss");
        try {
            long tDate = simpleDateFormat.parse(date).getTime();
            return dateFormat.format(tDate);
        } catch (ParseException e) {
            if (StringUtils.isEmpty(date)) {
                return "";
            } else {
                return date;
            }
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static String toShoppingStartTime(String date) {
        try {
            long tDate = new SimpleDateFormat(yyyyMMddHHmmss).parse(date).getTime();
            return new SimpleDateFormat(date_ymd).format(tDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return date;
        }
    }

    /**
     * 判断当天是否处于开始时间和结束时间的天数内
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 结果
     */
    public static boolean isMiddleOfTime(String startTime, String endTime) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long start = simpleDateFormat.parse(startTime).getTime();
            long end = simpleDateFormat.parse(endTime).getTime();
            if (simpleDateFormat.format(System.currentTimeMillis()).equals(simpleDateFormat.format(start))) {
                return true;
            } else if (simpleDateFormat.format(System.currentTimeMillis()).equals(simpleDateFormat.format(end))) {
                return true;
            } else return System.currentTimeMillis() >= start && System.currentTimeMillis() <= end;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 判断当天是否处于开始时间和结束时间的前后一小时内
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 结果
     */
    public static boolean isMiddleOfHourTime(String startTime, String endTime) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long start = simpleDateFormat.parse(startTime).getTime() - 60 * 60 * 1000;
            long end = simpleDateFormat.parse(endTime).getTime() + 60 * 60 * 1000;
            return System.currentTimeMillis() >= start && System.currentTimeMillis() <= end;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isDuringTime(String startTime, String endTime) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long start = simpleDateFormat.parse(startTime).getTime();
            long end = simpleDateFormat.parse(endTime).getTime();
            return System.currentTimeMillis() >= start && System.currentTimeMillis() <= end;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断时间是否在有效历史时间内（当前历史将保存7天）
     *
     * @param time 时间
     * @return 是否在期间内
     */
    @SuppressLint("SimpleDateFormat")
    public static boolean isDuringHistoryDate(String time) {
        try {
            Date mDate = new SimpleDateFormat(yyyyMMddHHmmss).parse(time);
            Date cDate = new SimpleDateFormat(yyyyMMddHHmmss).parse(getCurrentTime());
            long date = (cDate.getTime() - mDate.getTime()) / (24 * 60 * 60 * 1000);
            if (date >= 0) {
                return date <= 7;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 判断是否是公祭日
     *
     * @return 结果
     */
    public static boolean isSpecialDay() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return month == 11 && day == 13;
    }


    @SuppressLint("SimpleDateFormat")
    public static String BrokeDateFormat(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    /**
     * @param time 指定时间
     * @param type 1 天  2时  3分  4秒
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String restDate(String time, int type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long curTime = System.currentTimeMillis();
            long tagTime = simpleDateFormat.parse(time).getTime();
            long restTime = tagTime - curTime;
            switch (type) {
                case 1:
                    long restDay = restTime / (24 * 60 * 60 * 1000);
                    return restDay == 0 ? "00" : (restDay < 10 ? "0" + restDay : restDay + "");
                case 2:
                    long restHour = restTime % (24 * 60 * 60 * 1000) / (60 * 60 * 1000);
                    return restHour == 0 ? "00" : (restHour < 10 ? "0" + restHour : restHour + "");
                case 3:
                    long restMinute = restTime % (60 * 60 * 1000) / (60 * 1000);
                    return restMinute == 0 ? "00" : (restMinute < 10 ? "0" + restMinute : restMinute + "");
                case 4:
                    long restSecond = restTime % (60 * 1000) / 1000;
                    return restSecond == 0 ? "00" : (restSecond < 10 ? "0" + restSecond : restSecond + "");
                default:
                    return "00";
            }
        } catch (Exception e) {
            return "00";
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static long restDate(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long curTime = System.currentTimeMillis();
            long tagTime = simpleDateFormat.parse(time).getTime();
            return tagTime - curTime;
        } catch (Exception e) {
            return 1000;
        }
    }


}
